package com.liwenxu;

import android.util.Log;

import com.tutk.IOTC.AVAPIs;

import static java.lang.System.arraycopy;

/**
 * Created by 李文旭 on 2018/9/9.
 */

public class VideoThread implements Runnable {
    static final int VIDEO_BUF_SIZE = 100000;
    static final int FRAME_INFO_SIZE = 16;
    public static int stop=0;
    private int avIndex;

    public VideoThread(int avIndex) {
        this.avIndex = avIndex;
    }

    @Override
    public void run() {
        System.out.printf("[%s] \n",
                Thread.currentThread().getName());

        AVAPIs av = new AVAPIs();
        byte[] frameInfo = new byte[FRAME_INFO_SIZE];
        byte[] videoBuffer = new byte[VIDEO_BUF_SIZE];
        int[] pnActualFrameSize = new int[1];
        int[] ExpectedFrameSize = new int[1];
        int[] ActualFrameInfoSize = new int[1];
        while (stop==0) {
            int[] frameNumber = new int[1];
            int ret = av.avRecvFrameData2(avIndex,  videoBuffer,
                    VIDEO_BUF_SIZE, pnActualFrameSize, ExpectedFrameSize,
                    frameInfo, FRAME_INFO_SIZE,
                    ActualFrameInfoSize, frameNumber);
            //Log.d("shoudao " ,ret+" "+stop);
            if (ret >= 0) {
//                if(frameNumber[0]==0)//帧等于0 如果损坏会不会重新发呢？？？？存疑
//                {
//                    int pos=0;
//                    for(int i=4;i<pnActualFrameSize[0];i++)
//                    {
//                        if(videoBuffer[i]==0x00&&videoBuffer[i+1]==0x00&&videoBuffer[i+2]==0x00&&videoBuffer[i+3]==0x01&&videoBuffer[i+4]==0x68) {
//                            pos =i+ 4;
//                            break;
//                        }
//                    }
//                    int pos_two=0;
//                    int i=0;
//                    for(i=pos;i<pnActualFrameSize[0];i++)
//                    {
//                        if(videoBuffer[i]==0x00&&videoBuffer[i+1]==0x00&&videoBuffer[i+2]==0x00&&videoBuffer[i+3]==0x01&&videoBuffer[i+4]==0x65) {
//                            pos_two = i+4;
//                            break;
//                        }
//                    }
//                    if(i>=pnActualFrameSize[0])
//                    {
//                        Log.d("shoudao","nullospspps");
//                        continue;
//                    }
//                    if(pos_two>500)
//                    {
//                        byte newBuffer[]=new byte[pnActualFrameSize[0]];
//                        arraycopy(videoBuffer,0, newBuffer, 0,pnActualFrameSize[0]);
//                        VideoPlay.queue.add(new framInfo(newBuffer,pnActualFrameSize[0],frameNumber[0]));
//                        continue;
//                    }
//                    VideoPlay.mSps=new byte[pos-4];
//                    arraycopy(videoBuffer, 0, VideoPlay.mSps, 0, pos-4);
//                    VideoPlay.mPps=new byte[pos_two-pos];
//                    arraycopy(videoBuffer, pos-4, VideoPlay.mPps, 0,pos_two-pos);
//                    byte newBuffer[]=new byte[pnActualFrameSize[0]-pos_two+4];
//                    arraycopy(videoBuffer, pos_two-4, newBuffer, 0,pnActualFrameSize[0]-pos_two+4);
//                    int weightth = (H264SPSPaser.ue(VideoPlay.mSps, 34) + 1) * 16;
//                    int height = (H264SPSPaser.ue(VideoPlay.mSps, -1) + 1) * 16;
//                    Log.d("height ",weightth+" "+height);
//                    VideoPlay.queue.add(new framInfo(newBuffer,pnActualFrameSize[0]-pos_two+4,frameNumber[0]));
//                    Log.d("shoudao","shoudaospspps");
//                    continue;
            //}
                byte newBuffer[]=new byte[pnActualFrameSize[0]];
                arraycopy(videoBuffer,0, newBuffer, 0,pnActualFrameSize[0]);
                VideoPlay.queue.add(new framInfo(newBuffer,pnActualFrameSize[0],frameNumber[0]));
            } else {
                if (ret == AVAPIs.AV_ER_DATA_NOREADY) {
                    try {
                        Thread.sleep(30);
                        continue;
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                        break;
                    }
                } else if (ret == AVAPIs.AV_ER_LOSED_THIS_FRAME) {
                    System.out.printf("[%s] Lost video frame number[%d]\n",
                            Thread.currentThread().getName(), frameNumber[0]);
                    continue;
                } else if (ret == AVAPIs.AV_ER_INCOMPLETE_FRAME) {
                    System.out.printf("[%s] Incomplete video frame number[%d]\n",
                            Thread.currentThread().getName(), frameNumber[0]);
                    continue;
                } else if (ret == AVAPIs.AV_ER_SESSION_CLOSE_BY_REMOTE) {
                    System.out.printf("[%s] AV_ER_SESSION_CLOSE_BY_REMOTE\n",
                            Thread.currentThread().getName());
                    break;
                } else if (ret == AVAPIs.AV_ER_REMOTE_TIMEOUT_DISCONNECT) {
                    System.out.printf("[%s] AV_ER_REMOTE_TIMEOUT_DISCONNECT\n",
                            Thread.currentThread().getName());
                    break;
                } else if (ret == AVAPIs.AV_ER_INVALID_SID) {
                    System.out.printf("[%s] Session cant be used anymore\n",
                            Thread.currentThread().getName());
                    break;
                }
            }
        }
        //发送终止消息


        System.out.printf("[%s] Exit\n",
                Thread.currentThread().getName());
    }
}
